package admin;

import java.util.List;
import java.util.ResourceBundle;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import beans.AdminPageBean;
import utils.DomainDAORemote;
import utils.TechresourceDAORemote;
import utils.UserDAORemote;
import dtos.DomainDTO;

@ManagedBean(name = "domainBean")
@SessionScoped
public class DomainBean {
	private DomainDTO domainDto;

	ResourceBundle bundle = ResourceBundle.getBundle(
			"languageSettings.language", FacesContext.getCurrentInstance()
					.getViewRoot().getLocale());

	@EJB
	DomainDAORemote domainDaoRem;
	@EJB
	UserDAORemote ud;
	@EJB
	TechresourceDAORemote td;
	private List<DomainDTO> domainsList;
	private boolean editVisible = false;
	private boolean addVisible = true;

	private String message;

	private String domainSuccessAdditon = bundle
			.getString("Beans_admin_domainBean_domainSuccessAdditon");
	private String domainSuccessEdit = bundle
			.getString("Beans_admin_domainBean_domainSuccessEdit");
	private String domainFailRemove = bundle
			.getString("Beans_admin_domainBean_domainFailRemove");
	private String domainSuccessRemove = bundle
			.getString("Beans_admin_domainBean_domainSuccessRemove");

	@ManagedProperty("#{adminPageBean}")
	AdminPageBean apb;

	public AdminPageBean getApb() {
		return apb;
	}

	public void setApb(AdminPageBean apb) {
		this.apb = apb;
	}

	@PostConstruct
	public void init() {
		domainDto = new DomainDTO();
		message = "";
	}

	public DomainDTO getDomainDto() {
		return domainDto;
	}

	public void setDomainDto(DomainDTO domainDto) {
		this.domainDto = domainDto;
	}

	public void doUpdate(ActionEvent action) {
		updateDomain();
		this.domainsList = getDomainsList();
		clear();
		makeVisibleAdd();
		message = domainSuccessEdit;
	}

	public void doAdd(ActionEvent action) {
		domainDaoRem.insertDomain(domainDto);
		this.domainsList = getDomainsList();
		clear();
		message = domainSuccessAdditon;
	}

	public void removeListner(ActionEvent action) {
		int id = Integer.parseInt(action.getComponent().getAttributes()
				.get("domainID").toString());
		if (ud.getUsersByDomain(id).isEmpty()
				&& td.getTechResourcesDTOByDomainId(id).isEmpty()) {

			domainDaoRem.deleteDomain(domainDaoRem.getDomain(id));
			message = domainSuccessRemove;
		} else {
			message = domainFailRemove;
			this.domainsList = getDomainsList();
			clear();
		}
	}

	public void editListner(ActionEvent action) {
		int id = Integer.parseInt(action.getComponent().getAttributes()
				.get("domainID").toString());
		this.domainDto = domainDaoRem.getDomain(id);
		makeVisibleEdit();
		message = "";
		apb.setPage("domainCrud");
	}

	public String refresh() {

		return "mainAdmin";
	}

	public List<DomainDTO> getDomainsList() {
		this.domainsList = domainDaoRem.getAllDomains();
		return domainsList;
	}

	public void setDomainsList(List<DomainDTO> domainsList) {
		this.domainsList = domainsList;
	}

	public void updateDomain() {
		domainDaoRem.updateDomain(this.domainDto);
	}

	public void clear() {
		domainDto = new DomainDTO();
	}

	public boolean isEditVisible() {
		return editVisible;
	}

	public void setEditVisible(boolean editVisible) {
		this.editVisible = editVisible;
	}

	public boolean isAddVisible() {
		return addVisible;
	}

	public void setAddVisible(boolean addVisible) {
		this.addVisible = addVisible;
	}

	public void makeVisibleEdit() {
		editVisible = true;
		addVisible = false;
	}

	public void makeVisibleAdd() {
		editVisible = false;
		addVisible = true;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public String getDomainSuccess() {
		return domainSuccessAdditon;
	}

	public void setDomainSuccess(String domainSuccess) {
		this.domainSuccessAdditon = domainSuccess;
	}

	public String getDomainSuccessEdit() {
		return domainSuccessEdit;
	}

	public void setDomainSuccessEdit(String domainSuccessEdit) {
		this.domainSuccessEdit = domainSuccessEdit;
	}

}
